// pages/member/supplier/SupInfoManage/SupInfoManage.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    maxDate:"",
    valid_key:"",
    current_key:"",
    startDate: new Date('1900-01-01'),
    currentDate: new Date().getTime(),
    date_range_start:"",
    date_range_end:"",
    legal_range_start:"",
    legal_range_end:"",
    apply_info:[],
    apply_info_set:"",
    member_id: '',
    information: {
      my_name: '',
      phone_number: '',
      store_name: '',
      store_logo: '',
      bank_account: '',
      owner_name: '',
      opening_bank: '',
      opening_branch: '',
      enterprise_alipay: '',
      enterprise_alipay_user: '',
      alipay: '',
      alipay_username: '',
      weChat_account: '',
      category:'',
      group:'',
      imageList:''
    },
    imageUrl: '',
    jumpState: true,
    fill_set: '',
    // imageList:[]
    showLicense:false,
    show_license_valid_type:false,
    apply_info_form:{
      username:"",
      password:"",
      license_img:"",
      license_type:"",
      license_name:"",
      license_code:"",
      license_address_province:"",
      license_address_city:"",
      license_address_district:"",
      license_address_street:"",
      license_address_detail:"",
      license_valid_type:"",
      license_scope:"",
      license_date_start:"",
      branch_code:"",
      branch_name:"",
      license_date_end:"",
      legal_name:"",
      legal_address:"",
      legal_phone:'',
      legal_no:'',
      legal_valid_type:"",
      legal_date_start:"",
      legal_date_end:"",
      legal_img_front:"",
      legal_img_back:"",
      opening_permit:"",
      bank_account_name:"",
      bank_name:"",
      bank_card_no:"",
      owner_name:"",
      bank_sn:"",
      bank_code:"",
      bank_mp:"",        
      store_name:"",
      realname:"",
      mobile:"",
      remark:"",
      bank_type:"",
      company_bank:"",
      bank_username:"",
      bank_of_accounts:"",
      opening_branch:"",
      bank_img_front:"",
      bank_img_back:"",
      wechat:"",
      ali_username:"",
      ali:"",
      company_ali_username:"",
      company_ali:"",
    },
    show_bank:false,
    show_category:false,
    bank_info:[
      {title:"银行名称",key:'bank_name'},
      {title:"银行编号",key:'bank_code'},
      {title:"持卡人",key:'owner_name'},
      {title:"银行预留手机号",key:'bank_mp'}
    ],
    license_valid_type:[
      {id: 0,name: "非长期有效"},
      {id: 1,name: "长期有效"},
    ],
    license_type:[
      {id: 1, "name": "个体工商户"},
      {id: 2,"name": "有限责任公司"},
      {id: 3,"name": "其他"}
    ],
    bank_info_1:[
      {title:"银行账号",key:'company_bank'},
      {title:"开户人姓名",key:'bank_username'},
      {title:"开户行",key:'bank_of_accounts'},
      {title:"开户支行",key:'opening_branch'},

    ],
    bank_info_2:[
      {title:"银行账户名称",key:'bank_account_name'},
      {title:"银行卡号",key:'bank_card_no'},
      {title:"账户联行号",key:'bank_sn'},
      {title:"支行名称",key:"branch_name"},
      {title:"支行号",key:"branch_code"},
    ],

    choose_date_type:"",
    timeShow:false,
    street_name:"",

    apply_info: [],

    isShowStreet: false,
    districtVal: [],
    streetShow: false,

    //地区
    provinceData: [],
    cityData: [],
    districtData: [],
    pickerValue: [0, 0, 0],
    selectAddressData: [],
    showCity1: false,
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.member_id) {
      this.setData({
        member_id: options.member_id
      });
    }

    this._getFillSet();
    this._getJson();

    var maxDate = new Date();
    maxDate.setFullYear(maxDate.getFullYear() + 50);
    this.setData({maxDate})
  },
  _closeDateLw_1() {
    this.setData({
      'showCity1': false
    });
  },
  popup_handle(e){
    let key = e.currentTarget.dataset.key,
        flage = "";
    if(key=='license_type'){
      flage = "showLicense";
    }else if(key=='license_valid_type'||key=='legal_valid_type'){
      flage="show_license_valid_type"
      this.data.valid_key = key;
    }else if(key=='license_address'){
      flage="showCity1";
    }else if(key=='stree'){
      flage="streetShow";
    }else if(key=='category_id'){
      flage="show_category";
    }else if(key=='bank_info'){
      if(this.data.info.bank_info_edit==1){
        return
      }
      flage="show_bank";
    }
    this.setData({[flage]:true})
  },
  bindChange: function (e) {
    var val = e.detail.value;
    if (this.data.pickerValue[0] != val[0]) {
      val[1] = 0;
      val[2] = 0;
    }
    if (this.data.pickerValue[1] != val[1]) {
      val[2] = 0;
    }
    this.setData({
      "pickerValue[0]": val[0],
      "pickerValue[1]": val[1],
      "pickerValue[2]": val[2]
    });
    this._initAddressInfo();
  },
  //地址选择器数据初始化
  _initAddressInfo() {
    wx.getStorage({
      key: "provinceData",
      success:res => {
        this._getProvinceData();
      },
      fail: res=> {
        console.log("省fail");
        app._getNetWork({
          url: app.getNetAddresss("member.member-address.address"),
          success:res=> {
            var response = res.data;
            console.log(response,'response');
            var province = response.data.province;
            var city = response.data.city;
            var district = response.data.district;
            wx.setStorage({
              key: 'provinceData',
              data: province
            });
            wx.setStorage({
              key: 'cityData',
              data: city
            });
            wx.setStorage({
              key: 'districtData',
              data: district
            });
            this._initAddressInfo();
          },
          fail: function(res) {},
          state: true
        });
      }
    });
  },
  resultAdd_1() {
    let seleData = this.data.selectAddressData;
    this.setData({
      "apply_info_form.license_address_province":seleData[0].id,
      "apply_info_form.license_address_city":seleData[1].id,
      "apply_info_form.license_address_district":seleData[2].id,
      fullAddress: seleData[0].name + '-' + seleData[1].name + '-' + seleData[2].name,
      showCity1: false,
      street_name: '',
      districtVal: [],
    });
    this._getStreet(seleData[2].id);
  },
  // 读取省key
  _getProvinceData() {
    wx.getStorage({
      key: "provinceData",
      success:res=> {
        console.log("省读取成功", res.data);
        this.setData({provinceData: res.data});
        let areaname = '',id='';
        if(this.data.apply_info_form.license_address_province&&!this.data.showCity1){
          let _arr = res.data.filter(item=>item.id==this.data.apply_info_form.license_address_province);
          areaname =  _arr[0].areaname;
          id = _arr[0].id;
        }else{
          areaname = res.data[this.data.pickerValue[0]].areaname;
          id = res.data[this.data.pickerValue[0]].id;
        }
        this.setData({
          "selectAddressData[0].name": areaname,
          "selectAddressData[0].id": id,
          "selectAddressData[0].index": this.data.pickerValue[0]
        });
        this._getCityData();
      }
    });
  },
  // 读取城市key，筛选城市data
  _getCityData() {
    var _data = this.data.selectAddressData[0];
    console.log("城市", "_data", _data);
    var _keyName = 'cityData';
    wx.getStorage({
      key: _keyName,
      success:res=> {
        var _json = [];
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].parentid == _data.id) {
            _json.push(res.data[i]);
          }
        }
        console.log("城市数据筛选", _json);
        this.setData({ cityData: _json});
        let areaname = '',id='';
        if(this.data.apply_info_form.license_address_province&&!this.data.showCity1){
          let _arr = _json.filter(item=>item.id==this.data.apply_info_form.license_address_city);
          areaname = _arr[0].areaname;
          id = _arr[0].id;
        }else{
          areaname = _json[this.data.pickerValue[1]].areaname;
          id = _json[this.data.pickerValue[1]].id;
        }
        this.setData({
          "selectAddressData[1].name":areaname,
          "selectAddressData[1].id":id,
          "selectAddressData[1].index": this.data.pickerValue[1]
        });
        this._getDistrictData();
      }
    });
  },
  // 读取地区key，筛选地区data
  _getDistrictData () {
    var _data = this.data.selectAddressData[1];
    console.log("地区", "_data", _data);
    var _keyName = 'districtData';
    wx.getStorage({
      key: _keyName,
      success: res=> {
        console.log("地区读取成功");
        var _json = [];
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].parentid == _data.id) {
            _json.push(res.data[i]);
          }
        }
        console.log("地区数据筛选", _json);
        this.setData({districtData: _json});
        let areaname = '',id='';
        if(this.data.apply_info_form.license_address_province&&!this.data.showCity1){
          let _arr = _json.filter(item=>item.id==this.data.apply_info_form.license_address_district);
          areaname = _arr[0].areaname;
          id = _arr[0].id;
        }else{
          areaname = _json[this.data.pickerValue[2]].areaname;
          id =  _json[this.data.pickerValue[2]].id;
        }
        this.setData({
          "selectAddressData[2].name": areaname,
          "selectAddressData[2].id": id,
          "selectAddressData[2].index": this.data.pickerValue[2]
        });
        console.log("over", this.data.selectAddressData);
        let selectAddressData = this.data.selectAddressData;
        let fullAddress = `${selectAddressData[0].name}-${selectAddressData[1].name}-${selectAddressData[2].name}`;
        this.setData({fullAddress})
      }
    });
  },

  date_close(){
    this.setData({timeShow:false});
  },
      //获取街道
  _getStreet(param) {
    var that = this;
    let urlStr = app.getNetAddresss("member.memberAddress.street");
    urlStr += "&district_id=" + param;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1 && !app._isTextEmpty(res.data)) {
          if (!app._isTextEmpty(res.data) && res.data.length > 0) {
            that.setData({
              districtVal: res.data,
              isShowStreet: true,
            });
          } else {
            that.setData({
              isShowStreet: false,
            });
          }
        } else {
          that.setData({
            isShowStreet: false,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },
  onInput(e){
    let key = e.currentTarget.dataset.key;
    let _data = `apply_info_form.${key}`,
    value = e.detail.value;
    this.setData({[_data]:value});
  },
  input_handle(e){
    let _value = e.detail.value;
    let field = e.currentTarget.dataset.field;
    let revise = 'information.'+field;
    this.setData({[revise]:_value});
  },
  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {

  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  _getFillSet() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.supplier.frontend.info.getFillSet");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {},
      success: function(resdata) {
        let res = resdata.data;
        if (res.result == 1) {
          that.setData({
            'fill_set': res.data.fill_set ? res.data.fill_set : ''
          });
          console.log(that.data.fill_set);
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }

      },
      fail: function(res) {
        console.log(res.msg);
      }
    });
  },
  //获取数据
  _getJson() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.supplier.frontend.info.index");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        member_id: that.data.member_id
      },
      success: function(resdata) {
        var res = resdata.data;
        console.log(resdata,'resdata');
        if (res.result == 1) {
          
          that.setData({
            apply_info_set:res.data.apply_info_set,
            apply_info:res.data.apply_info,
            'information.my_name': res.data.realname ? res.data.realname : '',
            'information.phone_number': res.data.mobile ? res.data.mobile : '',
            'information.store_name': res.data.store_name ? res.data.store_name : "",
            'information.store_logo': res.data.logo ? res.data.logo : '',
            'imageUrl': res.data.logo ? res.data.logo : '',
            'information.bank_account': res.data.company_bank ? res.data.company_bank : '',
            'information.owner_name': res.data.bank_username ? res.data.bank_username : '',
            'information.opening_bank': res.data.bank_of_accounts ? res.data.bank_of_accounts : '',
            'information.opening_branch': res.data.opening_branch ? res.data.opening_branch : '',
            'information.enterprise_alipay': res.data.company_ali ? res.data.company_ali : '',
            'information.enterprise_alipay_user': res.data.company_ali_username ? res.data.company_ali_username : '',
            'information.alipay': res.data.ali ? res.data.ali : '',
            'information.alipay_username': res.data.ali_username ? res.data.ali_username : '',
            'information.weChat_account': res.data.wechat ? res.data.wechat : '',
            'information.category': res.data.has_one_category ? res.data.has_one_category.title : '默认行业',
            'information.group': res.data.has_one_group ? res.data.has_one_group.title : '默认分组',
            'information.imageList':res.data.has_one_aptitude?.aptitude ? res.data.has_one_aptitude?.aptitude : '',
            apply_info_set:res.data.apply_info_set,
            apply_info:res.data.apply_info||[],
            info:res.data
          });
          if(res.data.apply_info_set=='1'){
            let form = that.data.apply_info_form,
                _data = res.data,
                bank_info = [
              {title:"银行名称",key:'bank_name'},
              {title:"银行编号",key:'bank_code'},
              {title:"持卡人",key:'owner_name'},
              {title:"银行预留手机号",key:'bank_mp'}
            ]
            console.log(_data.bank_type,'_data.bank_type===');
            if(_data.bank_type==1){
              that.data.bank_info = bank_info.concat(that.data.bank_info_1);
            }else if(_data.bank_type==2){
              that.data.bank_info = bank_info.concat(that.data.bank_info_2);
            }else{
              that.data.bank_info = bank_info;
            }
            that.setData({bank_info:that.data.bank_info});
            for (const key in form) {
                if(key == 'license_img_url'){
                  that.data.apply_info_form.license_img = _data['license_img_url'];
                }else if(key=='license_date_end'){
                  that.setData({date_range_start:_data['license_date_end_str']})
                  that.data.apply_info_form['license_date_end']=_data['license_date_end'];
                }else if(key=='license_date_start'){
                  that.setData({date_range_start:_data['license_date_start_str']})
                  that.data.apply_info_form['license_date_start']=_data['license_date_start'];
                }else if(key=='legal_date_end'){
                  that.setData({legal_range_end:_data['legal_date_end_str']})
                  that.data.apply_info_form['legal_date_end']=_data['legal_date_end'];
                }else if(key=='legal_date_start'){
                  that.setData({legal_range_start:_data['legal_date_start_str']})
                  that.data.apply_info_form['legal_date_start']=_data['legal_date_start'];
                }else if(key=='legal_valid_type'||key=='license_valid_type'){
                  that.data.apply_info_form[key] = _data[key]
                }else if(_data[key]){
                  that.data.apply_info_form[key] = _data[key]
                }
            }
            that.setData({apply_info_form:that.data.apply_info_form});
            that._initAddressInfo();
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }

      },
      fail: function(res) {}
    });
  },
  //店铺名称值绑定
  storeNameinp(e) {
    let val = e.detail.value;
    this.setData({
      'information.store_name': val
    });
  },
  //选择图片
  onRead() {
    let that = this;
    let wx_token = wx.getStorageSync('wx_token');
    let session = wx.getStorageSync('session');
    let urlStr = app.getNetAddresss("plugin.supplier.frontend.info.upload");
    urlStr += '&3rd_session=' + session;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths[0]);
        wx.compressImage({
          src: tempFilePaths[0], // 图片路径
          quality: 60, // 压缩质量
          success(e) {
            let photourl = e.tempFilePath;
            console.log('eee', e);
            wx.uploadFile({
              url: urlStr,
              filePath: photourl,
              name: 'file',
              header:{
                "Content-Type": "multipart/form-data",
                Cookie: "PHPSESSID=" + wx_token,
              },
              success(resdata) {
                var res = JSON.parse(resdata.data);
                if (res.result == 1) {
                  that.setData({
                    imageUrl: photourl,
                    'information.store_logo': res.data.img
                  });
                } else {
                  wx.showToast({
                    icon: 'none',
                    title: res.msg,
                    duration: 1500
                  });
                }
              }
            });
          },
          fail(e) {

          },
          complete(e) {

          }
        });
      }
    });
  },
  //确认条件
  jumpJudge() {
    this.setData({
      jumpState: true
    });

    if (!this.data.information.my_name || !this.data.information.phone_number || !this.data.information.store_name || !this.data.information.store_logo) {
      this.setData({
        jumpState: false
      });
    }

    // for (let item in this.data.information) {
    // 	if (this.data.information[item] == '') {
    // 		this.setData({
    // 			jumpState: false
    // 		})
    // 	}
    // }

    if (this.data.fill_set) {
      if (this.data.fill_set.company_bank == "1") {
        if (this.data.information.bank_account == "") {
          this.setData({
            jumpState: false
          });
        }
      }
      if (this.data.fill_set.bank_username == "1") {
        if (this.data.information.owner_name == "") {
          this.setData({
            jumpState: false
          });
        }
      }
      if (this.data.fill_set.bank_of_accounts == "1") {
        if (this.data.information.opening_bank == "") {
          this.setData({
            jumpState: false
          });
        }
      }
      if (this.data.fill_set.opening_branch == "1") {
        if (this.data.information.opening_branch == "") {
          this.setData({
            jumpState: false
          });
        }
      }
      if (this.data.fill_set.company_ali == "1") {
        if (this.data.information.enterprise_alipay == "") {
          this.setData({
            jumpState: false
          });
        }
      }
      if (this.data.fill_set.company_ali_username == "1") {
        if (this.data.information.enterprise_alipay_user == "") {
          this.setData({
            jumpState: false
          });
        }
      }
      if (this.data.fill_set.ali == "1") {
        if (this.data.information.alipay == "") {
          this.setData({
            jumpState: false
          });
        }
      }
      if (this.data.fill_set.ali_username == "1") {
        if (this.data.information.alipay_username == "") {
          this.setData({
            jumpState: false
          });
        }
      }
      if (this.data.fill_set.wechat == "1") {
        if (this.data.information.weChat_account == "") {
          this.setData({
            jumpState: false
          });
        }
      }
    }

    if (this.data.imageUrl == '') {
      this.setData({
        jumpState: false
      });
    }
  },
  //确认修改
  modifyInformation() {
    var that = this;
    that.jumpJudge();
    if (that.data.jumpState) {
      if (app._isMoblie(that.data.information.phone_number)) {
        wx.showToast({
          icon: 'none',
          title: '请输入正确的手机号',
          duration: 1500
        });
        return;
      }
      if (this.data.fill_set.company_bank == '1') {
        if (!/^[0-9]*$/.test(that.data.information.bank_account)) {
          wx.showToast({
            icon: 'none',
            title: '请输入正确的银行卡账号',
            duration: 1500
          });
          return;
        }
      }

      if (this.data.fill_set.company_ali == '1') {
        if (!/^[a-zA-Z\d._-]*\@[a-zA-Z\d.-]{1,10}\.[a-zA-Z\d]{1,20}$/.test(that.data.information.enterprise_alipay)) {
          wx.showToast({
            icon: 'none',
            title: '请输入正确的企业支付宝账号',
            duration: 1500
          });
          return;
        }
      }

      if (this.data.fill_set.ali == '1') {
        if (!/^(?:1[3-9]\d{9}|[a-zA-Z\d._-]*\@[a-zA-Z\d.-]{1,10}\.[a-zA-Z\d]{1,20})$/.test(that.data.information.alipay)) {
          wx.showToast({
            icon: 'none',
            title: '请输入正确的支付宝账号',
            duration: 1500
          });
          return;
        }
      }

      if (this.data.fill_set.wechat == '1') {
        if (!/^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/.test(that.data.weChat_account)) {
          wx.showToast({
            icon: 'none',
            title: '请输入正确的微信号',
            duration: 1500
          });
          return;
        }
      }

      let json = {
        realname: that.data.information.my_name,
        mobile: that.data.information.phone_number,
        store_name: that.data.information.store_name,
        logo: that.data.information.store_logo,
        company_bank: that.data.information.bank_account,
        bank_username: that.data.information.owner_name,
        bank_of_accounts: that.data.information.opening_bank,
        opening_branch: that.data.information.opening_branch,
        company_ali: that.data.information.enterprise_alipay,
        company_ali_username: that.data.information.enterprise_alipay_user,
        ali: that.data.information.alipay,
        ali_username: that.data.information.alipay_username,
        wechat: that.data.information.weChat_account,
      };
      let urlStr = app.getNetAddresss("plugin.supplier.frontend.info.edit");
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: json,
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            wx.showToast({
              title: '修改成功',
              icon: 'none',
              duration: 1500,
              success: function() {
                setTimeout(function() {
                  //要延时执行的代码
                  wx.navigateBack({
                    delta: 1
                  });
                }, 1500); //延迟时间
              },
            });
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }

        },
        fail: function(res) {}
      });
    } else {
      wx.showToast({
        icon: 'none',
        title: '填写信息不完整',
        duration: 1500
      });
    }
  },
  onChangeBank(e){
    this.setData({"apply_info_form.bank_type":e.detail});
  },
  onChangeValid(e){
    if(this.data.valid_key == 'license_valid_type'){
      this.setData({"apply_info_form.license_valid_type":e.detail});
    }else{
      this.setData({"apply_info_form.legal_valid_type":e.detail});
    }
   
  },
  close_popup(e){
    let key =  e.currentTarget.dataset.key;
    if(key=='show_bank'){

      let bank_info = [
        {title:"银行名称",key:'bank_name'},
        {title:"银行编号",key:'bank_code'},
        {title:"持卡人",key:'owner_name'},
        {title:"银行预留手机号",key:'bank_mp'}
      ]
      if(this.data.apply_info_form.bank_type==1){
        this.data.bank_info = bank_info.concat(this.data.bank_info_1);
      }else if(this.data.apply_info_form.bank_type==2){
        this.data.bank_info = bank_info.concat(this.data.bank_info_2);
      }else{
        this.data.bank_info = bank_info;
      }
      this.setData({bank_info:this.data.bank_info});
    }
    this.setData({
      [key]:false
    })
  },
  onChange(e){
    this.setData({"apply_info_form.license_type":e.detail});
  },
  date_handle(e){
    let type = e.currentTarget.dataset.type,
        key  = e.currentTarget.dataset.key;
    this.data.choose_date_type = type;
    this.data.current_key = key;
    this.setData({timeShow:true});
  },
  on_input(event) {    
    let date  = event.detail;
    if(this.data.current_key=='legal_person'){
      if(this.data.choose_date_type=='start'){
        this.setData({
          "apply_info_form.legal_date_start": date,
          legal_range_start:this.timestampToTime(date)
        });
      }else{
        this.setData({
          "apply_info_form.legal_date_end": date,
          legal_range_end:this.timestampToTime(date)
        });
      }
    }else {
      if(this.data.choose_date_type=='start'){
        this.setData({ 
          "apply_info_form.license_date_start": date,
          date_range_start:this.timestampToTime(date)
        });
      }else{
        this.setData({ 
          "apply_info_form.license_date_end":date,
          date_range_end:this.timestampToTime(date)
        });
      }
    }

  },
  timestampToTime(timestamp) {

    var date = new Date(timestamp);
    let Y = date.getFullYear() + '-';
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    let D = this.change(date.getDate()) + ' ';
    return Y + M + D;
  },
  change(t) {
    if (t < 10) {
      return "0" + t;
    } else {
      return t;
    }
  },
  date_confrim(){
    this.setData({timeShow:false});
  },
  upload_permit(e){
    this.afterRead(e,'opening_permit')
  },
  after_legal_back(e){
    this.afterRead(e,'legal_img_back')
  },
  after_bank_font(e){
    this.afterRead(e,'bank_img_front')
  },
  after_bank_back(e){
    this.afterRead(e,'bank_img_back')
  },
  after_permit(e){
    this.afterRead(e,'opening_permit')
  },
  after_legal_font(e){
    this.afterRead(e,'legal_img_front')
  },
  after_license(e){
    this.afterRead(e,'license_img')
  },

  afterRead(event,key) {
    const { file } = event.detail;
    let urlStr = app.getNetAddresss('upload.uploadPic');
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: urlStr, 
      filePath: file.url,
      name: 'file',
      formData: null,
      success:res=> {
        let _res = JSON.parse(res.data);
        let img = `apply_info_form.${key}`;
        this.setData({ [img]:_res.data.img_url });
      },
    });
  },
  del_form_img(e){
    let key = e.currentTarget.dataset.key;
    let img = `apply_info_form.${key}`;
    this.setData({[img]:''})
  },
  form_handle(){
    let form = {...this.data.apply_info_form}
    let _json = {};
        for (const key in form) {
          if (form[key]||form[key]===0) {
            _json[key]=form[key];            
          }
        }
    if(_json.license_date_end&&(_json.license_date_end+'').length>10){
      _json.license_date_end = Math.floor(_json.license_date_end  / 1000)
    }
    if(_json.license_date_start&&(_json.license_date_start+'').length>10){
      _json.license_date_start = Math.floor(_json.license_date_start / 1000)
    }
    if(_json.legal_date_end&&(_json.legal_date_end+'').length>10){
      _json.legal_date_end = Math.floor(_json.legal_date_end / 1000)
    }
    if(_json.legal_date_start&&(_json.legal_date_start+'').length>10){
      _json.legal_date_start = Math.floor(_json.legal_date_start/ 1000)
    }

    if(this.data.apply_info_form.license_valid_type==1){
      _json.license_date_end&&delete _json.license_date_end;
    }
    if(this.data.apply_info_form.legal_valid_type==1){
      _json.legal_date_end&&delete _json.legal_date_end;
    }


    
    let urlStr = app.getNetAddresss("plugin.supplier.frontend.info.edit");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: _json,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: '修改成功',
            icon: 'none',
            duration: 1500,
            success: function() {
              setTimeout(function() {
                //要延时执行的代码
                wx.navigateBack({
                  delta: 1
                });
              }, 1500); //延迟时间
            },
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }

      },
      fail: function(res) {}
    });
  },

});
